package tool;

import java.io.IOException;

import reference.Property;
import reference.RecordError;
import reference.TokenMa;
import weibo4j.Friendships;
import weibo4j.Weibo;
import weibo4j.model.UserWapper;
import weibo4j.model.WeiboException;

public class MFollower {
	public static UserWapper getFollowById(String uid) throws InterruptedException, IOException{
		Weibo weibo = new Weibo();
		TokenMa aToken = new TokenMa();
		UserWapper users = new UserWapper();
		int next = 0;
		long totalnum = 0;
		int flagError = 0;
		
		do{
			weibo.setToken(aToken.getToken());
			Friendships fm = new Friendships();
			
			try {	
				UserWapper receive = fm.getFollowersById(uid,200,next);
				totalnum = receive.getTotalNumber();
				if(receive.getUsers().size()==0){
					if(flagError == 4){
						String des = uid+" get friends failed.";
						RecordError.WriteError(uid, des);
						return null;
						}
					else{
						flagError++;
						continue;
					}
					
				}
				users.addAll(receive.getUsers());		
				next += 200;
				
				
			}catch (WeiboException e) {
				e.printStackTrace();
				System.out.println(uid+ " UserWapper failed");
			} finally{
				Thread.sleep(Property.WAITTIME);
			}
		}while(next < totalnum);
		System.out.println(users.getTotalNumber());
			
		return users;
	} 
}
